<#include '../head.html'/>
<div style="width: 450px;margin-top: 20px;">
<form class="layui-form" action="" method="post" id="edit_form">
	<div class="layui-form-item">
    	<label class="layui-form-label">菜单名:</label>
    	<div class="layui-input-block">
    		<input type="hidden" name="id" value="${resource.id!}" readonly="readonly">
      		<input type="text" name="name" value="${resource.name}" required  lay-verify="required" placeholder="菜单名称" autocomplete="off" class="layui-input">
    	</div>
  	</div>
	<div class="layui-form-item">
    	<label class="layui-form-label">上级菜单:</label>
	    <div class="layui-input-block">
			<select name="pid"  lay-filter="resource_pid">
	        	<option value="0" selected="">顶级菜单</option>
				<#list resourceList as resource2>
				<option value="${resource2.id}"  <#if resource.pid==resource2.id>selected="selected"</#if> >&nbsp;&nbsp;&nbsp;&nbsp;${resource2.name}</option>
				<#if resource2.hasChildren>
				<#list resource2.children as children>
				<option value="${children.id}" title="2" <#if resource.pid==children.id>selected="selected"</#if> >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${children.name}</option>
				</#list>
				</#if>
				</#list>
	      </select>
	    </div>
  	</div>
  	<div class="layui-form-item">
    	<label class="layui-form-label">链接:</label>
    	<div class="layui-input-block">
      		<input type="text" name="resource" value="${resource.resource}" required  lay-verify="required" autocomplete="off" class="layui-input">
    	</div>
  	</div>
	<div class="layui-form-item">
    	<label class="layui-form-label">类型:</label>
    	<div class="layui-input-block">
      		<select name="rtype" id="rtype" disabled="disabled" lay-verify="required">
				<option value="0" <#if resource.rtype==0 > selected="selected" </#if> >菜单</option>
				<option value="1" <#if resource.rtype==1 > selected="selected" </#if> >按钮</option>
			</select>
    	</div>
  	</div>
  	<div class="layui-form-item">
    	<label class="layui-form-label">排序:</label>
    	<div class="layui-input-block">
      		<input type="text" name="rorder" value="${resource.rorder}" required  lay-verify="required"  autocomplete="off" class="layui-input">
    	</div>
  	</div>
  	<div class="layui-form-item">
    	<div class="layui-input-block">
      		<button class="layui-btn" lay-submit="" lay-filter="submit_info">提交</button>
      		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
    	</div>
  	</div>
</form>
</div>
<script src="/statics/layui/layui.js"></script>
<script type="text/javascript">
layui.use(['form', 'layedit', 'laydate'], function(){
	var form = layui.form
  	,layer = layui.layer
  	,layedit = layui.layedit
  	,laydate = layui.laydate;
    form.render();
  	form.on('select(resource_pid)', function (data) {  
  		category = data.value; 
  		lv=data.elem[data.elem.selectedIndex].title;
    	categoryName = data.elem[data.elem.selectedIndex].text;  
    	
    	if(lv=='2'){
    		$("#rtype").val(1);
    	}else{
    		$("#rtype").val(0);
    	}
    	
    	form.render('select');
    	
	});
  	
  //监听提交
    form.on('submit(submit_info)', function(data){
      	
      	$.ajax({
			type : 'POST',
			dataType : "json",
			url : "/resource/doCheckName",
			data : $("#edit_form").serialize(),
			cache : false,
			success : function(data) {
				if(data){
					layer.alert("该菜单名已经被使用了！");
					return;
				}
				saveResource();
				//location.reload();
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$.messager.progress('close');
				layer.alert(XMLHttpRequest.responseText);
	
			}
		});
      	
      	return false;
    });
  	
});

function saveResource(){
	$("#rtype").removeAttr("disabled");
	$.ajax({
		type : 'POST',
		dataType : "json",
		url : "/resource/saveEdit.jhtml",
		data : $("#edit_form").serialize(),
		cache : false,
		success : function(data) {
			if(data.state==0){
				layer.alert(
						data.message,
						{
							icon: 1,    // icon
							yes:function(){
								parent.location.reload();
							}
							
						}
				);
			}
			$("#rtype").attr("disabled","disabled");
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$("#rtype").attr("disabled","disabled");
			layer.alert(XMLHttpRequest.responseText);

		}
	});
	
}

$("#resource_pid").change(function(){
	var opt_= $(this).find("option:selected");
	if(opt_.attr("lv")==2){
		$("#rtype").val(1);
	}else{
		$("#rtype").val(0);
	}
})

$("#btn").click(function() {
	if (!$("#edit_form").form('validate')) {
		layer.alert("输入错误，请检查后重新保存");
		return;
	}
	
	$.ajax({
		type : 'POST',
		dataType : "json",
		url : "/resource/doCheckName",
		data : $("#edit_form").serialize(),
		cache : false,
		success : function(data) {
			if(data){
				layer.alert("该菜单名已经被使用了！");
				return;
			}
			saveResource();
			//location.reload();
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$.messager.progress('close');
			layer.alert(XMLHttpRequest.responseText);

		}
	});
	
});

</script>
